﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Spine;
using Spine.Unity;
using DG.Tweening;

namespace GameJam
{
	[RequireComponent(typeof(SpriteRenderer))]
	public class ScorePoint : MonoBehaviour 
	{
		bool _bClosed;
		public bool IsClosed {
			get{ return _bClosed; }
			set { 
				//if (_sprite == null)
				//	_sprite = this.GetComponent<SpriteRenderer> ();
				//_sprite.color = value ? Color.gray : Color.white;
				_bClosed = value;
				if (_animtor == null)
					InitAnimator ();
				_animtor.Play (value ? _uact_name : _burst_name);
			}
		}
		//SpriteRenderer _sprite;

		[SpineAnimation]
		public string red_grow;	
		[SpineAnimation]
		public string yellow_grow;
		[SpineAnimation]
		public string blue_grow;
		[SpineAnimation]
		public string purple_grow;

		ParticleSystem _ring;
		public ParticleSystem Ring 
		{
			get{ return _ring;}
		}

		SkeletonAnimation _spineAnim;

		Animator _animtor;

		string _uact_name = "unact";
		string _act_name = "act";
		string _burst_name = "burst";
		string _charge_name = "charge";
		string _charged_name = "charged";

		int _uact_hash;
		int _act_hash;
		int _burst_hash;
		int _charge_hash;
		int _charged_hash;

		Vector3 _focusPoint;

		// Use this for initialization
		void Start () 
		{
			//if (_sprite == null)
			//	_sprite = this.GetComponent<SpriteRenderer> ();

			_spineAnim = this.GetComponentInChildren<SkeletonAnimation> ();
			_ring = this.GetComponentInChildren<ParticleSystem> ();

			_focusPoint = this.transform.transform.Find ("focusPoint").position;

			InitAnimator ();
		}

		void InitAnimator()
		{
			_animtor = this.GetComponent<Animator> ();
			_uact_hash = Animator.StringToHash (_uact_name);
			_act_hash = Animator.StringToHash (_act_name);
			_burst_hash = Animator.StringToHash (_burst_name);
			_charge_hash = Animator.StringToHash (_charge_name);
			_charged_hash = Animator.StringToHash (_charged_name);

			_animtor.Play (_uact_name);
		}
		
		// Update is called once per frame
		void Update ()
		{
			
		}

		void PlayerEnter(TreePlayer player)
		{
			if (IsClosed)
				return;

			if (player.Energy >= 3) {
				this.IsClosed = true;
				//_sprite.color = player.BaseColor;
				//_sprite.enabled = false;

				PlaySpine (player.player_index);
				player.Score++;
				player.Energy = 0;
				player.Die ();

				_animtor.Play (_charge_name);

				StartCoroutine (FocusMode(player));
				//CameraCtrl.Instance.IsInFocusMode = true;
			}
        }

		IEnumerator FocusMode(TreePlayer player)
		{
			Vector3 camePos = CameraCtrl.Instance.transform.position;

			CameraCtrl.Instance.IsInFocusMode = true;
			_focusPoint.z = camePos.z;
			CameraCtrl.Instance.transform.DOMove (_focusPoint, 1.0f);
			GameCtrl.Instance.SetAllPlayerCouldControl (false);
			yield return new WaitForSeconds (2.0f);

            if (!GameCtrl.Instance.IsGameOver)
            {
                CameraCtrl.Instance.transform.DOMove(camePos, 1.0f);
                CameraCtrl.Instance.IsInFocusMode = false;
                yield return new WaitForSeconds(1.0f);
                GameCtrl.Instance.SetAllPlayerCouldControl(true);
            }
            //player.CouldControl = false;

        }

		void PlaySpine(int player_index)
		{
			string anim = string.Empty;
			if (player_index== 0) {
				anim = red_grow;
			} else if (player_index == 1) {
				anim = blue_grow;
			} else if (player_index == 2) {
				anim = yellow_grow;
			} else if (player_index == 3) {
				anim = purple_grow;
			}

			_spineAnim.state.SetAnimation (0, anim, false);
			_spineAnim.state.AddAnimation (0, anim + "n", true, 0);
		}

        public void ResetSpine()
        {
			//_sprite.enabled = true;
			//_sprite.color = Color.white;

            _spineAnim.state.SetAnimation(0, "default", true);
        }

		void OnTriggerEnter2D(Collider2D other)
		{
			if (other.gameObject.tag == "Player") 
			{
				TreePlayer player = other.gameObject.GetComponent<TreePlayer> ();
				PlayerEnter (player);
			}
		}
	}
}
